import Mock from 'mockjs'
import { userList } from './data'

// 登录接口
Mock.mock('/api/login', 'post', function (options) {
  const { username, password } = JSON.parse(options.body)
  const user = userList.find((item) => item.username === username && item.password === password)
  if (user) {
    return {
      code: 200,
      data: {
        token: user.token,
        userInfo: {
          id: user.id,
          username: user.username,
          nickname: user.nickname,
          avatar: user.avatar,
          menus: user.menus,
        },
      },
      message: 'success',
    }
  }
  return {
    code: 400,
    data: {},
    message: '用户名或密码错误',
  }
})
// 动态路由菜单接口
Mock.mock('/api/menu/routes', 'post', function (options) {
  const { token } = JSON.parse(options.body)
  let routes = []
  // 超级管理员路由
  if (token === 'admin_token_1234567890') {
    routes = [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: 'Layout',
        redirect: '/dashboard/workplace',
        meta: {
          title: '仪表板',
          icon: 'Dashboard',
          order: 1,
          breadcrumb: false,
          roles: ['admin', 'editor', 'user'],
        },
        children: [
          {
            path: 'workplace',
            name: 'Workplace',
            component: 'views/dashboard/Workplace',
            meta: {
              title: '工作台',
              icon: 'Monitor',
              noCache: false,
            },
          },
          {
            path: 'analysis',
            name: 'Analysis',
            component: 'views/dashboard/Analysis',
            meta: {
              title: '分析页',
              icon: 'TrendCharts',
              noCache: false,
            },
          },
        ],
      },
      {
        path: '/system',
        name: 'System',
        component: 'Layout',
        redirect: '/system/user',
        meta: {
          title: '系统管理',
          icon: 'Setting',
          order: 2,
          roles: ['admin'],
        },
        children: [
          {
            path: 'user',
            name: 'SystemUser',
            component: 'views/system/User',
            meta: {
              title: '用户管理',
              icon: 'User',
              permissions: ['system:user:view'],
            },
          },
          {
            path: 'role',
            name: 'SystemRole',
            component: 'views/system/Role',
            meta: {
              title: '角色管理',
              icon: 'Avatar',
              permissions: ['system:role:view'],
            },
          },
          {
            path: 'menu',
            name: 'SystemMenu',
            component: 'views/system/Menu',
            meta: {
              title: '菜单管理',
              icon: 'Menu',
              permissions: ['system:menu:view'],
            },
          },
          {
            path: 'dept',
            name: 'SystemDept',
            component: 'views/system/Dept',
            meta: {
              title: '部门管理',
              icon: 'OfficeBuilding',
              permissions: ['system:dept:view'],
            },
          },
          {
            path: 'dict',
            name: 'SystemDict',
            component: 'views/system/Dict',
            meta: {
              title: '字典管理',
              icon: 'Collection',
              permissions: ['system:dict:view'],
            },
          },
        ],
      },
      {
        path: '/content',
        name: 'Content',
        component: 'Layout',
        redirect: '/content/article',
        meta: {
          title: '内容管理',
          icon: 'Document',
          order: 3,
          roles: ['admin', 'editor'],
        },
        children: [
          {
            path: 'article',
            name: 'ContentArticle',
            component: 'views/content/Article',
            meta: {
              title: '文章管理',
              icon: 'EditPen',
              permissions: ['content:article:view'],
            },
          },
          {
            path: 'category',
            name: 'ContentCategory',
            component: 'views/content/Category',
            meta: {
              title: '分类管理',
              icon: 'FolderOpened',
              permissions: ['content:category:view'],
            },
          },
          {
            path: 'tag',
            name: 'ContentTag',
            component: 'views/content/Tag',
            meta: {
              title: '标签管理',
              icon: 'PriceTag',
              permissions: ['content:tag:view'],
            },
          },
          {
            path: 'comment',
            name: 'ContentComment',
            component: 'views/content/Comment',
            meta: {
              title: '评论管理',
              icon: 'ChatDotRound',
              permissions: ['content:comment:view'],
            },
          },
        ],
      },
      {
        path: '/tools',
        name: 'Tools',
        component: 'Layout',
        redirect: '/tools/generator',
        meta: {
          title: '开发工具',
          icon: 'Tools',
          order: 4,
          roles: ['admin'],
        },
        children: [
          {
            path: 'generator',
            name: 'ToolsGenerator',
            component: 'views/tools/Generator',
            meta: {
              title: '代码生成',
              icon: 'Connection',
              permissions: ['tools:generator:view'],
            },
          },
          {
            path: 'build',
            name: 'ToolsBuild',
            component: 'views/tools/Build',
            meta: {
              title: '表单构建',
              icon: 'Grid',
              permissions: ['tools:build:view'],
            },
          },
          {
            path: 'swagger',
            name: 'ToolsSwagger',
            component: 'views/tools/Swagger',
            meta: {
              title: 'API文档',
              icon: 'Document',
              permissions: ['tools:swagger:view'],
            },
          },
        ],
      },
      {
        path: '/monitor',
        name: 'Monitor',
        component: 'Layout',
        redirect: '/monitor/online',
        meta: {
          title: '系统监控',
          icon: 'Monitor',
          order: 5,
          roles: ['admin'],
        },
        children: [
          {
            path: 'online',
            name: 'MonitorOnline',
            component: 'views/monitor/Online',
            meta: {
              title: '在线用户',
              icon: 'UserFilled',
              permissions: ['monitor:online:view'],
            },
          },
          {
            path: 'job',
            name: 'MonitorJob',
            component: 'views/monitor/Job',
            meta: {
              title: '定时任务',
              icon: 'Timer',
              permissions: ['monitor:job:view'],
            },
          },
          {
            path: 'server',
            name: 'MonitorServer',
            component: 'views/monitor/Server',
            meta: {
              title: '服务监控',
              icon: 'Platform',
              permissions: ['monitor:server:view'],
            },
          },
          {
            path: 'cache',
            name: 'MonitorCache',
            component: 'views/monitor/Cache',
            meta: {
              title: '缓存监控',
              icon: 'Coin',
              permissions: ['monitor:cache:view'],
            },
          },
        ],
      },
      {
        path: '/log',
        name: 'Log',
        component: 'Layout',
        redirect: '/log/operation',
        meta: {
          title: '日志管理',
          icon: 'List',
          order: 6,
          roles: ['admin'],
        },
        children: [
          {
            path: 'operation',
            name: 'LogOperation',
            component: 'views/log/Operation',
            meta: {
              title: '操作日志',
              icon: 'EditPen',
              permissions: ['log:operation:view'],
            },
          },
          {
            path: 'login',
            name: 'LogLogin',
            component: 'views/log/Login',
            meta: {
              title: '登录日志',
              icon: 'Key',
              permissions: ['log:login:view'],
            },
          },
          {
            path: 'error',
            name: 'LogError',
            component: 'views/log/Error',
            meta: {
              title: '错误日志',
              icon: 'CircleClose',
              permissions: ['log:error:view'],
            },
          },
        ],
      },
    ]
  }
  // 测试角色路由
  else if (token === 'test_token_1234567891') {
    routes = [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: 'Layout',
        redirect: '/dashboard/workplace',
        meta: {
          title: '仪表板',
          icon: 'Dashboard',
          order: 1,
          breadcrumb: false,
          roles: ['admin', 'editor', 'user'],
        },
        children: [
          {
            path: 'workplace',
            name: 'Workplace',
            component: 'views/dashboard/Workplace',
            meta: {
              title: '工作台',
              icon: 'Monitor',
              noCache: false,
            },
          },
        ],
      },
      {
        path: '/content',
        name: 'Content',
        component: 'Layout',
        redirect: '/content/article',
        meta: {
          title: '内容管理',
          icon: 'Document',
          order: 2,
          roles: ['admin', 'editor'],
        },
        children: [
          {
            path: 'article',
            name: 'ContentArticle',
            component: 'views/content/Article',
            meta: {
              title: '文章管理',
              icon: 'EditPen',
              permissions: ['content:article:view'],
            },
          },
          {
            path: 'category',
            name: 'ContentCategory',
            component: 'views/content/Category',
            meta: {
              title: '分类管理',
              icon: 'FolderOpened',
              permissions: ['content:category:view'],
            },
          },
          {
            path: 'tag',
            name: 'ContentTag',
            component: 'views/content/Tag',
            meta: {
              title: '标签管理',
              icon: 'PriceTag',
              permissions: ['content:tag:view'],
            },
          },
        ],
      },
    ]
  }
  // 普通用户路由
  else if (token === 'user_token_1234567892') {
    routes = [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: 'Layout',
        redirect: '/dashboard/workplace',
        meta: {
          title: '仪表板',
          icon: 'Dashboard',
          order: 1,
          breadcrumb: false,
          roles: ['admin', 'editor', 'user'],
        },
        children: [
          {
            path: 'workplace',
            name: 'Workplace',
            component: 'views/dashboard/Workplace',
            meta: {
              title: '工作台',
              icon: 'Monitor',
              noCache: false,
            },
          },
        ],
      },
      {
        path: '/html5',
        name: 'Html5',
        component: 'Layout',
        redirect: '/html5/canvas',
        meta: {
          title: 'HTML5',
          icon: 'Html5',
          order: 2,
          roles: ['admin', 'editor', 'user'],
        },
        children: [
          {
            path: 'canvas',
            name: 'Html5Canvas',
            component: 'views/html5/Canvas',
            meta: {
              title: '画布',
              icon: 'Canvas',
              noCache: false,
              hidden: false,
            },
          },
          {
            path: 'swiper',
            name: 'Html5Swiper',
            component: 'views/html5/Swipers',
            meta: {
              title: '轮播图',
              icon: 'Swiper',
              noCache: false,
              hidden: false,
            },
          },
        ],
      },
      {
        path: '/vuejs',
        name: 'Vuejs',
        component: 'Layout',
        redirect: '/vuejs/slot',
        meta: {
          title: 'Vuejs',
          icon: 'Vuejs',
          order: 3,
          roles: ['admin', 'editor', 'user'],
        },
        children: [
          {
            path: 'slot',
            name: 'VuejsSlot',
            component: 'views/vuejs/Slot',
            meta: {
              title: '插槽',
              icon: 'Slot',
              noCache: false,
              hidden: false,
            },
          },
          {
            path: 'dynamicslot',
            name: 'VuejsDynamicslot',
            component: 'views/vuejs/Dynamicslot',
            meta: {
              title: '动态插槽',
              icon: 'Dynamicslot',
              noCache: false,
              hidden: false,
            },
          },
        ],
      }
    ]
  }

  return {
    code: 200,
    data: routes,
    message: '获取路由成功',
  }
})
// 表格数据接口
Mock.mock('/api/table/data', 'get', function (options) {
  return {
    code: 200,
    data: {
      // mock一千条数据
      list: Array.from({ length: 1000 }, (_, index) => ({
        id: index + 1,
        date: `2025-01-${index + 1}`,
        name: `name${index + 1}`,
        address: `address${index + 1}`,
      })),
      total: 100,
    },
    message: '获取表格数据成功',
  }
})
console.log('🎭 Mock服务已启动')
